package com.jcl.recruit.channel.service;

import com.jcl.core.constants.Constant;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.channel.logic.ChannelNetLogic;
import com.jcl.recruit.channel.model.ChannelNet;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ChannelNetService

 * @Author yyliu
 * @Date 2018/9/11 17:12
 * @Version 1.0
 **/
@Service
public class ChannelNetService {

    private static final Logger logger = LoggerFactory.getLogger(ChannelNetService.class);

    @Autowired
    private ChannelNetLogic channelNetLogic;


    public List<ChannelNet> selectList() {
        String corpCode = RequestUtils.getCorpCode();
        String userId = RequestUtils.getUserId();
        return channelNetLogic.selectList(corpCode, userId);

    }


    public ChannelNet findOne(Integer id) {
        String corpCode = RequestUtils.getCorpCode();
        return channelNetLogic.findOne(corpCode, id);
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 17:23 2018/9/11
     * @Param [channelNet]
     * @return int
     **/
    public int addChannelNet(ChannelNet channelNet) {

        String corpCode = RequestUtils.getCorpCode();
        String userId = RequestUtils.getUserId();

        channelNet.setCorpCode(corpCode);
        channelNet.setAddUser(Integer.parseInt(userId));
        channelNet.setModifyTime(new Date());
        channelNet.setCreateTime(new Date());
        // 默认 开启
        channelNet.setStatus(Constant.NUMBER_1);

        //RedisCacheUtils.addChanelAccount(channelNet);

        return channelNetLogic.addChannelNet(channelNet);
    }


    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 17:19 2018/9/11
     * @Param [channelNet]
     * @return int
     **/
    public int updateChannelNet(ChannelNet channelNet) {

        String corpCode = RequestUtils.getCorpCode();

        ChannelNet oldChannel = channelNetLogic.findOne(corpCode, channelNet.getId());
        try {
            PropertyUtils.copyProperties(oldChannel, channelNet);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            //e.printStackTrace();
        }

        oldChannel.setModifyTime(new Date());

        // RedisCacheUtils.addChanelAccount(oldChannel);

        return channelNetLogic.updateChannelNet(oldChannel);
    }


    public int deleteChannelNet(Integer id) {
        String corpCode = RequestUtils.getCorpCode();
        ChannelNet oldChannel = channelNetLogic.findOne(corpCode, id);
        // 删除为 2
        return channelNetLogic.deleteChannelNet(corpCode, id, 2);
    }
}
